package io.gitee.xyuesheng.day02;

import org.junit.Test;

import java.util.Arrays;
import java.util.Scanner;

public class HomeworkWeekOne {
    //随机数输出数组月份元素
    @Test
    public void testMonths() {
        String[] names = {" ", "一月", "二月",
                "三月", "四月", "五月", "六月",
                "七月", "八月", "九月", "十月",
                "十一月", "十二月"};
        int a = (int) (Math.random() * 12 + 1);
        System.out.println(a);
        System.out.println(names[a]);
    }

    //编写一个方法，计算一个整数各位数字之和
    @Test
    public void testSum1() {
        int n = 1234;
        System.out.println(sumDigits1(n));
    }

    public static int sumDigits1(long n) {
        int sum = 0;
        while (n != 0) {
            long m = n % 10;
            sum += m;
            n /= 10;
        }
        return sum;
    }

    @Test
    public void testSum2() {
        int n = 12345;
        System.out.println(sumDigits2(n));
    }

    public static int sumDigits2(long n) {
        int sum = 0;
        int count = 0;
        count = getNumber(n);
        for (int i = 1; i <= count; i++) {
            long m = n % 10;
            sum += m;
            n /= 10;
        }
        return sum;
    }

    public static int getNumber(long n) {
        int count = 0;
        while (n > 0) {
            n = n / 10;
            count++;
        }
        return count;
    }

    //编写一个方法，合并两个数组
    @Test
    public void testMerge() {
        int[] array1 = {1, 5, 16};
        int[] array2 = {3, 4, 6, 7};
        System.out.println(Arrays.toString(merge(array1, array2)));
    }

    public static int[] merge(int[] a1, int[] a2) {
        int[] temp = new int[a1.length + a2.length];
        int m = 0;
        int n = 0;
        for (int i = 0; i < a1.length + a2.length; i++) {
            if (m < a1.length && n < a2.length) {
                if (a1[m] <= a2[n]) {
                    temp[i] = a1[m++];
                } else {
                    temp[i] = a2[n++];
                }
            } else if (m > a1.length) {
                temp[i] = a2[n++];
            } else {
                temp[i] = a1[m++];
            }
        }
        return temp;
    }

    //编写一个方法，求矩阵中某一列元素之和
    @Test
    public void testMatric() {
        double[][] matric = {
                {1.5, 2, 3, 4},
                {2.3, 5, 8, 9},
                {3, 5.4, 7, 10}
        };
        System.out.println(sumColumn(matric, 0));
        System.out.println(sumColumn(matric, 1));
        System.out.println(sumColumn(matric, 2));
        System.out.println(sumColumn(matric, 3));
    }

    public static double sumColumn(double[][] matrix, int column) {
        double sum = 0;
        for (int i = 0; i < 3; i++) {
            double n = matrix[i][column];
            sum += n;
        }
        return sum;
    }

    @Test
    public void testMatric1() {
        double[][] matric = {
                {1.5, 2, 3, 4},
                {2.3, 5, 8, 9},
                {3, 5.4, 7, 10}
        };
        System.out.println(Arrays.toString(sumColumn1(matric)));
    }

    public static double[] sumColumn1(double[][] matrix) {
        double[] sum = new double[matrix[0].length];
        for (int j = 0; j < sum.length; j++) {
            for (int i = 0; i < matrix.length; i++) {
                sum[j] += matrix[i][j];
            }
        }
        return sum;
    }


}
